<?php 

class App_Model_DbTable_Article_Type_Image_Data extends Ext_Db_Table  
{  
    protected $_name = 'article_type_image_data';
    
    private static $_imageDataRow = array();
    
	public function getRowById($id)
	{
		if(!$id)
		{
			return "";
		}
		
	    if(isset(self::$_imageDataRow['id'][$id]))
		{
			return self::$_imageDataRow['id'][$id];
		}
		
		$db = $this->getAdapter();
		$safe = $db->quoteInto(" id = ? ", $id);
		$row = $this->fetchRow($safe);
		if (!$row) {
			//throw new Exception("Count not find row $id");
			return "";
		}
		$_imageDataRow = $row->toArray();
		
		self::$_imageDataRow['id'][$id] = $_imageDataRow;
		
		return $_imageDataRow;
	}

	public function getAllById($idArray)
	{
		if(!$idArray)
		{
			return "";
		}
		
		$db = $this->getAdapter();
		$safe = $db->quoteInto(" id IN(?) ", $idArray);
		$all = $this->fetchAll($safe);
		if (!$all) {
			//throw new Exception("Count not find row $id");
			return "";
		}
		return $all->toArray();
	}

	public function del($_imageDataId)
	{
		if(!$id)
		{
			return "";
		}
		
		$this->_unsetImageDataRow($_imageDataId);
		
		$db = $this->getAdapter();
		$safe = $db->quoteInto(" id = ? ", $_imageDataId);
		$rows_affected = $this->delete($safe);
		
		return $rows_affected;
	}
	
	public function addNew($inputArray)
	{
		$sql_data_array = array();
		if(array_key_exists('articleId', $inputArray)) $sql_data_array['articleId'] = $inputArray['articleId'];
		if(array_key_exists('imageId', $inputArray)) $sql_data_array['imageId'] = $inputArray['imageId'];
		if(array_key_exists('title', $inputArray)) $sql_data_array['title'] = $inputArray['title'];
		if(array_key_exists('description', $inputArray)) $sql_data_array['description'] = $inputArray['description'];
		if(array_key_exists('htmlOn', $inputArray)) $sql_data_array['htmlOn'] = $inputArray['htmlOn'];

		try {
			// 插入数据行并返回插入的行数
			$rows_affected = $this->insert($sql_data_array);
			$dataId = $this->getAdapter()->lastInsertId();
		}catch (Exception $e){
			trigger_error($e->getMessage(), E_USER_WARNING);
		}
		
		return $dataId;	
	}
	
	public function addOld($inputArray, $_imageDataId)
	{
		$sql_data_array = array();
		if(array_key_exists('description', $inputArray)) $sql_data_array['description'] = $inputArray['description'];
		if(array_key_exists('htmlOn', $inputArray)) $sql_data_array['htmlOn'] = $inputArray['htmlOn'];
		if(!$sql_data_array)
		{
			return "";
		}
		
		try {
			$db = $this->getAdapter();
			$where = $db->quoteInto(" id = ? ", $_imageDataId);
			$rows_affected = $this->update($sql_data_array, $where);
			if($rows_affected){
			    $this->_unsetImageDataRow($_imageDataId);
			}
		}catch (Exception $e){
			trigger_error($e->getMessage(), E_USER_WARNING);
		}
		
		return "";
	}
	
    private function _unsetImageDataRow($_imageDataId)
    {
        if(!$_imageDataId)
        {
            return "";
        }
        
        $_imageDataRow = $this->getRowById($_imageDataId);
        if(!$_imageDataRow)
        {
            return "";
        }
        
        unset(self::$_controlRow['id'][$_controlId]);
    }
}